#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "Log.h"
#include "comm.h"

const std::string gpath = "./";
const std::string gname = "dict.txt";
const std::string gsep = ": ";

using namespace LogMudule;

class Dictionary
{
private:
    bool LoadDiction()
    {
        std::string file = _path + _name;
        std::ifstream in(file.c_str());
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file" << file << "error";
            return false;
        }
        std::string line;
        while (std::getline(in, line))
        {
            std::string key;
            std::string value;
            if (SplitString(line, &key, &value, gsep))
            {
                _map.insert(std::make_pair(key, value));
            }
        }
        in.close();
        return true;
    }

public:
    Dictionary(const std::string &path = gpath, const std::string &name = gname)
        : _path(path), _name(name)
    {
        LoadDiction();
        Print();
    }
    std::string Translate(const std::string &word)
    {
        auto iter = _map.find(word);
        if (iter == _map.end())
        {
            return "None";
        }
        return iter->second;
    }
    void Print()
    {
    }
    ~Dictionary()
    {
    }

private:
    std::unordered_map<std::string, std::string> _map;
    std::string _path;
    std::string _name;
};